//
//  KY1008AdvertView.m
//  KYMobClick
//
//  Created by 01 on 16/11/8.
//  Copyright © 2016年 Czj. All rights reserved.
//

#import "KY1008AdvertView.h"

@interface KY1008AdvertView ()
@property (nonatomic, strong) UILabel     *titleLab;
@property (nonatomic, strong) UILabel     *textLab;
@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) NSMutableDictionary      *dict;
@end

@implementation KY1008AdvertView

#pragma mark - Init
- (instancetype)init {
    self = [super init];
    if (self) {
        self.titleLab = [[UILabel alloc] init];
        self.textLab = [[UILabel alloc] init];
        self.textLab.numberOfLines = 3;
        self.imgView = [[UIImageView alloc] init];
        
        [self addSubview:self.titleLab];
        [self addSubview:self.textLab];
        [self addSubview:self.imgView];
        
        [self createTap];
    }
    return self;
}

#pragma mark - Public methods
/**
 根据内容填充视图
 */
- (void) createContent:(NSDictionary *)dataDic blockHeight:(void (^)(NSInteger))viewHeight {
    self.dict = [NSMutableDictionary dictionaryWithDictionary:dataDic];
    NSString *pathDocument = [KYSystemInfo getSaveAdvertImagePath];
    NSString *imgUrl;
    NSString *title;
    NSString *text;
    NSString *imagePath;
    if (dataDic && dataDic[@"data"]) {
        imgUrl = dataDic[@"data"][@"imageurl"];
        title = dataDic[@"data"][@"headline"];
        text =  dataDic[@"data"][@"description"];
        imagePath = [pathDocument stringByAppendingString:[NSString stringWithFormat:@"%@.png",self.dict[@"data"][@"adsid"]]];
    }
    CGFloat titleX = KY_MARGIN;
    CGFloat titleY = KY_MARGIN;
    CGFloat titleW = kDeviceWidth - 2.5 * KY_MARGIN - kCurrentScreenWidth(140);
    
    self.titleLab.text = title;
    self.titleLab.font = kSystemFont(kDefaultfontsize);
    self.titleLab.frame = CGRectMake(titleX, titleY, titleW, 0);
    [self.titleLab sizeToFit];
    
    CGFloat textX = titleX;
    CGFloat textY = CGRectGetMaxY(self.titleLab.frame) + KY_MARGIN / 2.;
    CGFloat textW = titleW;
    
    self.textLab.font = kSystemFont(kDefaultfontsize);
    self.textLab.text = text;
    self.textLab.frame = CGRectMake(textX, textY, textW, 0);
    [self.textLab sizeToFit];
    
    CGFloat imgX = kDeviceWidth - kCurrentScreenWidth(140) - KY_MARGIN / 2.;
    CGFloat imgY = KY_MARGIN;
    CGFloat imgW = kCurrentScreenWidth(140);
    CGFloat imgH = kCurrentScreenWidth(90);
    self.imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
    UIImage *cacheImage = [UIImage imageWithContentsOfFile:imagePath];
    [self.imgView KYSD_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:cacheImage options:KYSDWebImageAllowInvalidSSLCertificates progress:nil completed:^(UIImage *image, NSError *error, KYSDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
            CGSize size = image.size;
            CGFloat height= size.height;
            CGFloat width = size.width;
//          KYLog(@"左文右图广告高度：%f；宽度：%f",height,width);
            CGRect frame = _imgView.frame;
            frame.size.height = frame.size.width * height / width;
            _imgView.frame = frame;
            _viewHeight = (CGRectGetMaxY(self.imgView.frame) >= CGRectGetMaxY(self.textLab.frame) ? CGRectGetMaxY(self.imgView.frame) : CGRectGetMaxY(self.textLab.frame)) + KY_MARGIN;
            if (viewHeight) viewHeight(_viewHeight);
            [self postPVData];
        }else{
//            KYLog(@"左文右图广告加载失败");
        }
    }];
}
/**
 添加手势
 */
- (void)createTap {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changePage)];
    [self addGestureRecognizer:tap];
}

#pragma mark - Response methods
/**
 处理点击事件
 */
- (void) changePage {
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickView:)]) {
        [self.delegate clickView:self.dict[@"data"]];
    }
}
/**
 统计PV数据
 */
- (void)postPVData {
    //发送统计数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"advertPVData" object:self.dict[@"data"]];
}
#pragma mark - Lazy load
- (NSMutableDictionary *)dict {
    if (!_dict) {
        _dict = [[NSMutableDictionary alloc] init];
    }
    return _dict;
}

@end
